package com.cloud.controller.coupon;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bean.coupon.BatchCoupon;
import com.cloud.bean.user.User;
import com.cloud.common.BaseReturn;
import com.cloud.service.coupon.BatchCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import static com.cloud.common.JwtUtils.getByToken;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuyuan
 * @since 2022-08-06
 */
@RestController
@RequestMapping("/batch-coupon")
public class BatchCouponController {
    @Autowired
    BatchCouponService batchCouponService;

    @RequestMapping("/create")
    public BaseReturn create(@RequestBody FormBatchCoupon formBatchCoupon){
        User user = getByToken();
        if(user == null){
            return BaseReturn.build("4003","用户异常请你联系管理员");
        }
        String name = formBatchCoupon.getName();
        if(name == null){
            return BaseReturn.build("5000","请输入优惠卷的名称");
        }
        String denomination = formBatchCoupon.getDenomination();
        if(denomination == null){
            return BaseReturn.build("5000","请输入优惠卷面额");
        }

        formBatchCoupon.setCustomerOn(user.getCustomerNo());
        formBatchCoupon.setOperatorId(user.getId());

       boolean result =  batchCouponService.create(formBatchCoupon);
       if(result){
           return BaseReturn.build("2000","创建成功");
       }else {
           return BaseReturn.build("5000","创建失败,联系工作人员");
       }

    }
    /**
     * 发布优惠券
     *  params id
     * */
    @RequestMapping("/push")
    public BaseReturn push(Long id){
        return batchCouponService.push(id);
    }

    @RequestMapping("queryList")
    public BaseReturn queryList(@RequestBody FormBatchCoupon formBatchCoupon){
        Page<BatchCoupon> page = new Page<>(formBatchCoupon.getPage(), formBatchCoupon.getSize());
        QueryWrapper<BatchCoupon> queryWrapper = new QueryWrapper<>(formBatchCoupon);
        IPage<BatchCoupon> pageList = batchCouponService.page(page, queryWrapper);
        return BaseReturn.build("2000","查询成功",pageList);
    }

    @RequestMapping("/getById")
    public BaseReturn getById(Long id){
        BatchCoupon byId = batchCouponService.getById(id);
        if(byId == null){
            return BaseReturn.build("5000","数据关联错误");
        }
        return BaseReturn.build("2000","查询成功",byId);
    }
    @RequestMapping("/updateRules")
    public BaseReturn updateRules(@RequestBody FormBatchCoupon formBatchCoupon){
        //1.根据批次去规则查出来 有那些规则
        //2.拿出传过的规则(一个保留的规则，一个是删除的规则)
        return batchCouponService.updateRules(formBatchCoupon);

    }



}

